package cn.e_eap.esite.manager;

import java.util.List;
import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import cn.e_eap.esite.web.form.PagerForm;

@Component
public class BaseManager<T> extends HibernateDaoSupport {

	@Resource(name = "sessionFactory")
	public void setSuperSessionFactory(SessionFactory sf) {
		super.setSessionFactory(sf);
	}

	public void add(T o) {
		getHibernateTemplate().save(o);
	}

	public void update(T o) {
		getHibernateTemplate().save(o);
	}

	final protected void remove(Class<T> clazz, String cond) {
            cond = cond.replaceAll("@", " object_0 ");
            this.getSession().createQuery(
				"delete from " + clazz.getName() + " object_0  " + cond).executeUpdate();
	}
	

	final protected void removeByIds(Class<T> clazz, String ids) {
            this.getSession().createQuery(
				"delete from " + clazz.getName() +
				" object_0 where object_0.id in("+ids+")")
				.executeUpdate();
	}

	final protected T findUnique(Class<T> clazz) {
		return findUnique(clazz, null);
	}

	final protected T findUnique(Class<T> clazz, String condition) {
		List<T> list = null;
		final String hql = " from " + clazz.getName() + " object_0 ";

		final StringBuilder cond = new StringBuilder();

		if (condition != null && condition.trim().length() > 0) {
			cond.append((condition).replaceAll("@", " object_0 "));
		}

		list = this.getHibernateTemplate().find(hql + cond);

		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	final protected List<T> getPageModel(String hql, PagerForm pf) {
		Session session = getSession();
		List<T> list = null;
		if (pf == null || pf.getNumPerPage() == 0) {
			list = session.createQuery(hql).list();
			pf.setTotalCount(list.size());
			return list;
		}

		final int pageSize = pf.getNumPerPage(), currentPage = pf.getPageNum();
		final int first = pageSize * (currentPage - 1);
		Query q = session.createQuery(hql);
		list = q.setFirstResult(first).setMaxResults(pageSize).list();
		String hqlCount = "select count(*) "
				+ hql.substring(hql.indexOf("from"));
		Long count = (Long) session.createQuery(hqlCount).uniqueResult();
		if (count == null) {
                    return null;
                }
		pf.setTotalCount(count);

		return list;
	}

	final protected List<T> getPageModel(final Class<T> clazz,
			final String condition, PagerForm pf) throws RuntimeException {

		Session session = getSession();

		String hql = " from " + clazz.getName() + " object_0 ";
		StringBuilder cond = new StringBuilder();
		if (condition != null && condition.trim().length() > 0) {
			cond.append(" where 1=1 ");
			cond.append((condition).replaceAll("@", " object_0 "));
		}
		hql = hql + cond;

		Query q = getSession().createQuery(hql);
		if (pf == null || pf.getNumPerPage() == 0) {
			return q.list();
		}

		int pageSize = pf.getNumPerPage();
		int currentPage = pf.getPageNum();

		final int first = pageSize * (currentPage - 1);

		List<T> list = q.setFirstResult(first).setMaxResults(pageSize).list();
		Long count = (Long) session.createQuery("select count(*) " + hql)
				.uniqueResult();
		if (count == null)
			pf.setTotalCount(0);
		else
			pf.setTotalCount(count);
		return list;

	}

	final protected List<T> getPageModel(final Class<T> clazz, PagerForm pf) {
		return this.getPageModel(clazz, null, pf);
	}
}
